/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package serverv2_0;
import java.io.* ;
import java.net.* ;
import java.util.* ;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
 *
 * @author suhui
 */
final class TCPReceive1 implements Runnable {

    final static String CRLF="\r\n";

    String regex_c = "action:\\w+;";
    String regex_u = "user:\\w+;";
    String regex_pw = "password:\\w+;";
    String regex_exp = "exp_gained:\\w+;";

    players playerlist;

    Socket socket;
    int status = 0;
    
            public TCPReceive1(Socket socket) throws Exception{
                this.socket = socket;
            }

            public void run()
            {
                try
                {
                    processRequest();
                }
                catch (Exception e)
                {
                    System.out.println(e);
                }

            }

            private void processRequest() throws Exception{
                BufferedReader inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
                String receivedsentence = inFromClient.readLine();

                switch(findtype(receivedsentence))
                {
                    case 1: processcommand(receivedsentence);
                            break;
                    case 2: processdata(receivedsentence);
                            break;
                    case 3: status = 0;
                            break;
                    case 4: status = 6;
                            
                            break;
                            
                }
                sendstatus(status);


       }
            private int findtype(String sentence)
            {
                String REGEX = "type:\\w+;";
                Pattern p = Pattern.compile(REGEX);
                Matcher m = p.matcher(sentence);
                m.find();
                String commandtype = (sentence.substring(m.start(),m.end())).substring(5);

                System.out.println(commandtype);

                if (commandtype.equals("command;"))
                {
                    return 1;
                }
                else if (commandtype.equals("data;"))
                {
                    return 2;

                }
                else if (commandtype.equals("reply;"))
                {
                    return 3;
                }
                 else
                     return 4;

            }
            void processcommand(String sentence)
            {
                
                Pattern p = Pattern.compile(regex_c);
                Matcher m = p.matcher(sentence);
                m.find();
                
                String action = (sentence.substring(m.start(),m.end()-1)).substring(8);
                System.out.println(action);

                p = Pattern.compile(regex_u);
                m = p.matcher(sentence);
                m.find();

                String user = (sentence.substring(m.start(),m.end()-1)).substring(5);

                System.out.println(user);
                
                if(action.equals("register")||action.equals("login"))
                {
                    p = Pattern.compile(regex_pw);
                    m = p.matcher(sentence);
                    m.find();
                    
                    String password = (sentence.substring(m.start(),m.end()-1)).substring(9);

                    //if(action.equals("register"))
                       // playerlist.addnew(user, password , socket.getInetAddress(), socket.getPort() );
                    //else
                        //playerlist.login(user, password,socket.getInetAddress(), socket.getPort() ) ;

                    //boolean done = playermanage(action,user,password,0);
                }
                else if (action.equals("send_client_list"))
                {

                }
                else
                        sendstatus(6);
                        
            }
            boolean processdata(String sentence)
            {
                Pattern p = Pattern.compile(regex_u);
                Matcher m = p.matcher(sentence);
                m.find();

                String user = (sentence.substring(m.start(),m.end()-1)).substring(5);

                p = Pattern.compile(regex_exp);
                m = p.matcher(sentence);
                m.find();

                int exp = Integer.parseInt((sentence.substring(m.start(),m.end()-1)).substring(11));

                //boolean done = playermanage("data",user,"",exp);

                return true;
            }
            void sendstatus(int type)
            {
                String reply = "type:reply;\nstatus:";
                switch(type)
                {
                    case 1: reply = "ok;";
                    break;
                    case 2: reply = "wrong_pass;";
                    break;
                    case 3: reply = "invalid_user;";
                    break;
                    case 4: reply = "reg_successful;";
                    break;
                    case 5: reply = "reg_unsuccessful;";
                    break;
                    case 100: reply = "error;";
                    break;
                    default: reply="";
                    break;

                }
                
            }
}
